jQuery(function($) {
    let orderGridHeight = getGridHeight();
    //初始化日期选择控件
    $('.date-picker').datepicker({
        todayHighlight: true,
        language: 'cn',
        autoclose: true
    }).next().on(ace.click_event, function(){
        $(this).prev().focus();
    });

    let gridSelector = "#gridTable", pagerSelector = "#gridPager";
    let parentColumn = $(gridSelector).closest('[class*="col-"]');
    //resize to fit page size
    $(window).on('resize.jqGrid', function () {
        $(gridSelector).jqGrid( 'setGridWidth', parentColumn.width());
    });

    //resize on sidebar collapse/expand
    $(document).on('settings.ace.jqGrid' , function(ev, event_name, collapsed) {
        if( event_name === 'sidebar_collapsed' || event_name === 'main_container_fixed' ) {
            //setTimeout is for webkit only to give time for DOM changes and then redraw!!!
            setTimeout(function() {
                $(gridSelector).jqGrid( 'setGridWidth', parentColumn.width());
            }, 20);
        }
    });

    jQuery(gridSelector).jqGrid({
        url : '/sameCityOrder/listSameCityOrder',
        jsonReader : {
            root: "list",    //数据集
            page: 'pageNum', //当前页号
            total: 'pages',  //总页数
            records: "total" //总记录数
        },
        rownumbers: true,
        shrinkToFit:false,
        autoScroll: true,
        mtype : "post",
        datatype: 'json',
        //公式：orderGridHeight - (蓝色标题行数 - 1) * 40
        height: (orderGridHeight - 40),
        colNames:['单号','标题','下单用户ID','状态','下单时间','服务时间','省份','城市','区/县','联系电话','金额(元)','类型','支付方式','地址'],
        colModel:[
            {name:'soBillNumber',index:'soBillNumber', width:100},
            {name:'soName',index:'soName',width:160,formatter: function(nameValue, options, rowObj){
                return htmlEncodeByRegExp(nameValue);
            }},
            {name:'soUserId',index:'soUserId', width:90},
            {name:'soStatus',index:'soStatus', width:60,formatter: function(statusCellValue){
                if (statusCellValue == '0'){
                    return '未抢';
                }else if (statusCellValue == '1'){
                    return '已抢';
                }else if (statusCellValue == '2' || statusCellValue == '3' || statusCellValue == '7'){
                    return '已完成';
                }else if (statusCellValue == '4'){
                    return '未确认';
                }else if (statusCellValue == '5'){
                    return '已取消';
                }else if (statusCellValue == '6'){
                    return '已过期';
                }
                return statusCellValue;
            }},
            {name:'soOrderDate',index:'soOrderDate', width:150},
            {name:'soServiceDate',index:'soServiceDate', width:150},
            {name:'provinceName',index:'provinceName', width:60},
            {name:'cityName',index:'cityName', width:60},
            {name:'areaName',index:'areaName', width:80},
            {name:'soPhone',index:'soPhone', width:100},
            {name:'soAmount',index:'soAmount', width:70},
            {name:'serviceTypeName',index:'serviceTypeName', width:70},
            {name:'soPaymentWay',index:'soPaymentWay', width:60,formatter: function(pwCellValue){
                if (pwCellValue == '0'){
                    return '线下';
                }else if (pwCellValue == '1'){
                    return '微信';
                }else if (pwCellValue == '2'){
                    return '支付宝';
                }else if (pwCellValue == '3'){
                    return '银联';
                }
                return pwCellValue;
            }},
            {name:'soAddress',index:'soAddress', width:200,formatter: function(addressValue){
                return htmlEncodeByRegExp(addressValue);
            }}
        ],
        rowNum:10,
        rowList:[10,20,30],
        pager : pagerSelector,
        altRows: true,
        loadComplete : function() {
            let table = this;
            setTimeout(function(){
                updatePagerIcons(table);
            }, 0);
        }
    });
    //trigger window resize to make the grid get the correct size
    $(window).triggerHandler('resize.jqGrid');

    //navButtons
    jQuery(gridSelector).jqGrid('navGrid',pagerSelector,
        { 	//navbar options
            edit: false,
            editicon : 'ace-icon fa fa-pencil blue',
            add: false,
            addicon : 'ace-icon fa fa-plus-circle purple',
            del: false,
            delicon : 'ace-icon fa fa-trash-o red',
            search: true,
            searchicon : 'ace-icon fa fa-search orange',
            refresh: true,
            refreshicon : 'ace-icon fa fa-refresh green',
            view: false,
            viewicon : 'ace-icon fa fa-search-plus grey',
        }
    );

    //replace icons with FontAwesome icons like above
    function updatePagerIcons(table) {
        let replacement = {
            'ui-icon-seek-first' : 'ace-icon fa fa-angle-double-left bigger-140',
            'ui-icon-seek-prev' : 'ace-icon fa fa-angle-left bigger-140',
            'ui-icon-seek-next' : 'ace-icon fa fa-angle-right bigger-140',
            'ui-icon-seek-end' : 'ace-icon fa fa-angle-double-right bigger-140'
        };
        $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function(){
            let icon = $(this);
            let $class = $.trim(icon.attr('class').replace('ui-icon', ''));
            if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
        });
    }
    //查询按钮
    $('#spQueryOrder').click(function () {
        let orderStartDate = $('#qOrderStartDate').val(),currentDate = getCurrentDate(),
            orderEndDate = $('#qOrderEndDate').val(),serviceStartDate = $('#qServiceStartDate').val(),
            serviceEndDate = $('#qServiceEndDate').val();
        if (!isNull(orderStartDate)) {
            if (!checkDate(orderStartDate)) {
                swal('错误提示','下单开始日期格式错误','error');
                return;
            }
            if (orderStartDate > currentDate) {
                swal('错误提示','下单开始日期不能大于当前日期','error');
                return;
            }
        }
        if (!isNull(orderEndDate) && !checkDate(orderEndDate)) {
            swal('错误提示','下单结束日期格式错误','error');
            return;
        }
        if (!isNull(serviceStartDate) && !checkDate(serviceStartDate)) {
            swal('错误提示','服务开始日期格式错误','error');
            return;
        }
        if (!isNull(serviceEndDate) && !checkDate(serviceEndDate)) {
            swal('错误提示','服务结束日期格式错误','error');
            return;
        }
        let orderParam = {
            billNo: $('#qOrderNo').val(),
            soName: $('#qName').val(),
            soStatus: $('#qStatus').val(),
            phone: $('#qOrderPhone').val(),
            orderStartDate: orderStartDate,
            orderEndDate: orderEndDate,
            serviceStartDate: serviceStartDate,
            serviceEndDate: serviceEndDate
        };
        jQuery(gridSelector).setGridParam({postData:orderParam}).trigger("reloadGrid");
    });
});